<?php
/**
 * Created by PhpStorm.
 * User: ciwei
 * Date: 2017/12/4
 * Time: 8:20
 */
namespace app\index\controller;


use app\index\model\Answer;
use app\index\model\Collect;
use app\index\model\Vercode;
use \think\Controller;
use \think\Request;
use \think\Loader;

class Question extends Common
{
    public function index()
    {
        db('question')->where('qid',input('qid'))->setInc('view_num');

        $qid = Request::instance()->param('qid');
        $question = new \app\index\model\Question();
        $data = $question->getQuestion($qid);
        $collect = new Collect();

        $data['tag'] = $collect->getTag($qid);
        $answer = new Answer();
        $answer = $answer ->getAnswer($qid);
        foreach ($answer as $k=>$v){
            $answer[$k]['zan'] = db('zan')
                ->where([
                    'uid'=>session('uid'),
                    'aid' => $v['aid'],
                ])
                ->find();
        }
        $indexHot = $question->indexHot();
        return $this->fetch('',[
            'title' => '文章首页',
            'data' => $data,
            'answer' => $answer,
            'indexHot' => $indexHot,
        ]);
    }

    public function add()
    {
        $cates = Loader::model('Cate')->select();
        $model = new Vercode();
        $question = $model->getRand();

        return $this->fetch('',[
            'title' => '文章添加',
            'cates' => $cates,
            'question' => $question,
        ]);
    }
    public function addcheck()
    {
        $data = Request::instance()->param();
        $code = new Vercode();
        $code->checkCode($data['vercode']);
        $validate = Loader::validate('Question');
        if(!$validate->check($data)){
            exit(json_encode(['error'=>1,'info'=>$validate->getError()]));
        }
        $question = new \app\index\model\Question();
        $question->addQuestion($data);
    }

    public function shoucang()
    {
        $data = Request::instance()->param();


        if(session('uid') ==$data['duid']){
            exit(json_encode(['error'=>1,'info'=>'不能收藏自己的问题']));
        }
        $collect = new Collect();
        $collect->checkCollect($data);
    }

    public function qlist()
    {
        $where = [];
        if (input('cid')){
            $where['q.cid'] = ['=',input('cid')];
        }
        if(input('status')){
            if (input('status') == 'jing'){
                $where['q.is_jing'] = ['=','1'];
            }
            if (input('status') == 'over'){
                $where['q.status'] = ['=','1'];
            }
            if (input('status') == 'notover'){
                $where['q.status'] = ['=','0'];
            }
        }
        $question = new \app\index\model\Question();
        $data = $question->getlist($where);
        $cate = \think\DB::table('cate')->select();
        $hotlist = $question->getHot();
        $this->view->engine->layout('newlayout');
        return $this->fetch('',[
           'title'=>'文章列表',
            'question' => $question,
            'data' =>$data,
            'cate'=>$cate,
            'hotlist' => $hotlist,
        ]);
    }
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $newfile = '/think/public/uploads/'.$info->getSaveName();
            echo json_encode(['code'=>0,'msg'=>"",'data'=>['src'=>$newfile]]);

        } else {
            // 上传失败获取错误信息
            echo json_encode(['code'=>1,'msg'=>$file->getError()]);
        }
    }
}